<template>
  <view class="container">

    <!-- 头像区域 -->
    <view class="avatar-container">
      <view class="avatar-wrapper">
        <image 
          class="avatar" 
          :src="avatarUrl" 
          mode="widthFix"
          @click="chooseAvatar"
        ></image>
       <!-- <view class="avatar-upload-btn" @click.stop="chooseAvatar">
          <uni-icons type="camera" size="18" color="#666"></uni-icons>
        </view> -->
      </view>
    </view>

    <!-- 表单区域 -->
    <view class="form-container">
      <!-- 昵称 -->
      <view class="form-item">
        <view class="form-label">昵称</view>
        <input 
          class="form-input" 
          placeholder="请输入昵称" 
          v-model="userName"
          placeholder-class="placeholder-style"
        ></input>
      </view>

      <!-- 手机 -->
      <view class="form-item">
        <view class="form-label">手机</view>
        <view class="form-value">{{phoneNumber}}</view>
      </view>

    </view>

    <!-- 法律提示 -->
    <view class="legal-notice">
      根据未成年人保护相关法律法规的要求，我们不主动处理14周岁以下未成年人的个人信息。如果您为14周岁以下的用户，请填写您的个人资料。
    </view>

    <!-- 保存按钮 -->
    <view class="save-btn" @click="saveInfo">
      <text class="btn-text">保存</text>
    </view>

    <!-- 退出登录 -->
        <view class="logout-btn" @click="logout">
          退出登录
        </view>

    <!-- 确认对话框 -->
    <uni-dialog 
      title="提示" 
      content="确定要退出登录吗？" 
      :show="showLogoutDialog"
      @confirm="logout"
      @cancel="cancelLogout"
    ></uni-dialog>
  </view>
</template>

<script>
	import { mapState } from 'vuex';
export default {
  data() {
    return {
      // 用户信息
      avatarUrl: '/static/carbon-fiber.png',
      userName: '',
      phoneNumber:'',
      login: false,
      // 对话框状态
      showLogoutDialog: false
    };
  },
  computed: {
  	...mapState(['userInfo', "appProperties"])
  },
  onShow() {
    // 可以在这里从本地存储或接口加载用户信息
    this.loadUserInfo();
  },
  methods: {
    // 获取今天的日期
    getToday() {
      const date = new Date();
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    
    // 加载用户信息
    loadUserInfo() {
      // 实际项目中应该从接口或本地存储获取
      if (this.userInfo) {
        this.userName = this.userInfo.userName || '';
        this.phoneNumber = this.userInfo.phoneNumber || '';
		this.login = true

      }
    },
    
    // 返回上一页
    navigateBack() {
      uni.navigateBack({
        delta: 1
      });
    },
    
    // 选择头像
    chooseAvatar() {
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          const tempFilePath = res.tempFilePaths[0];
          // 这里可以上传图片到服务器，成功后更新头像
          this.avatarUrl = tempFilePath;
          
          // 添加头像更换动画
          const avatar = this.$refs.avatar;
          if (avatar) {
            avatar.classList.add('avatar-change-animation');
            setTimeout(() => {
              avatar.classList.remove('avatar-change-animation');
            }, 500);
          }
        }
      });
    },
    


    
    // 保存信息
    saveInfo() {
		if (!this.login) {
			uni.navigateTo({
				url:`/pages/login/login`
			})
		}
		
      if (!this.userName) {
        uni.showToast({
          title: '请输入昵称',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      
      // 保存用户信息
	  const user = this.userInfo
	  user.userName = this.userName
	  user.avatar = this.avatarUrl
		let that = this;
      uni.showModal({
      	content: "是否保存?",
      	cancelText: "取消",
      	confirmText: "保存",
      	success: function(e) {
      		if(e.confirm){
      			that.$request("api/v1/saveUserInfo", "put",user).then(result => {
      				that.$store.commit("setUserInfo",user)
					// 显示保存成功提示
					uni.showToast({
					  title: '保存成功',
					  icon: 'success',
					  duration: 2000
					});
      			})
				
      		}
      	}
      })
    },
    
    // 确认退出登录
    confirmLogout() {
      this.showLogoutDialog = true;
    },
    
    // 取消退出登录
    cancelLogout() {
      this.showLogoutDialog = false;
    },
    
    // 退出登录
    logout() {
		console.log("退出登录")
      this.showLogoutDialog = false;
      // 清除用户信息
      // 退出登录
      	let that = this;
      	if(!this.userInfo){
      		this.$msg("请先登录")
      		return;
      	}
      	uni.showModal({
      		content: "是否退出登录?",
      		cancelText: "取消",
      		confirmText: "退出登录",
      		success: function(e) {
      			if(e.confirm){
      				that.$request("api/v1/logout", "delete").then(result => {
      					that.$store.commit("logout")
      					uni.reLaunch({
      						url:'/pages/login/login'
      					})
      				})
      			}
      		}
      	})
     
    }
  }
};
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 导航栏样式 */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}

.back-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  font-weight: 500;
  color: #333;
  flex: 1;
  text-align: center;
}

.right-actions {
  display: flex;
  align-items: center;
}

.action-icon {
  margin-left: 30rpx;
}

/* 头像区域 */
.avatar-container {
  display: flex;
  justify-content: center;
  padding: 60rpx 0;
  background-color: #fff;
}

.avatar-wrapper {
  position: relative;
  width: 200rpx;
  height: 200rpx;
}

.avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  transition: all 0.3s ease;
}

.avatar-change-animation {
  animation: avatarChange 0.5s ease;
}

@keyframes avatarChange {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.avatar-upload-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 50rpx;
  height: 50rpx;
  background-color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

/* 表单区域 */
.form-container {
  background-color: #fff;
  margin-top: 20rpx;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1px solid #f5f5f5;
}

.form-label {
  width: 160rpx;
  font-size: 32rpx;
  color: #333;
}

.form-input {
  flex: 1;
  font-size: 32rpx;
  color: #333;
  padding: 10rpx 0;
}

.placeholder-style {
  color: #ccc;
}

.form-value {
  flex: 1;
  font-size: 32rpx;
  color: #333;
}

/* 性别选择 */
.gender-options {
  flex: 1;
  display: flex;
  gap: 60rpx;
}

.radio-label {
  display: flex;
  align-items: center;
  font-size: 32rpx;
  color: #333;
}

.radio {
  margin-right: 15rpx;
  transform: scale(1.2);
}

/* 生日选择 */
.birthday-wrapper {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.birthday-value {
  font-size: 32rpx;
  color: #333;
}

/* 法律提示 */
.legal-notice {
  padding: 30rpx;
  font-size: 26rpx;
  color: #999;
  line-height: 1.6;
  margin-top: 20rpx;
  background-color: #fff;
}

/* 保存按钮 */
.save-btn {
  margin: 60rpx 30rpx;
  height: 100rpx;
  background-color: #FF3B30;
  border-radius: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5rpx 15rpx rgba(255, 59, 48, 0.3);
  transition: all 0.3s ease;
}

.save-btn:active {
  transform: scale(0.98);
  box-shadow: 0 3rpx 10rpx rgba(255, 59, 48, 0.2);
}

.btn-text {
  font-size: 36rpx;
  color: #fff;
  font-weight: 500;
}

/* 退出登录 */
.logout-btn {
  margin-top: auto;
  padding: 30rpx;
  text-align: center;
  font-size: 32rpx;
  color: #666;
  border-top: 1px solid #f5f5f5;
  background-color: #fff;
  height: 100rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logout-btn:active {
  background-color: #f9f9f9;
}


</style>
